package com.syn.synold4s.util;

import static com.syn.synold4s.constants.TagCode.code360;
import static com.syn.synold4s.constants.TagCode.codeDriver;
import static com.syn.synold4s.constants.TagCode.codeLock;
import static com.syn.synold4s.constants.TagCode.codeTowerCrane;
import static com.syn.synold4s.constants.TagCode.codeUnSafeAct;
import static com.syn.synold4s.constants.TagCode.codeWatch;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.syn.synold4s.R;
import com.syn.synold4s.bean.dto.MapAllDeviceDto;
import com.syn.synold4s.control.dialog.GoogleMapDriverDialog;
import com.syn.synold4s.control.dialog.GoogleMapLockDialog;
import com.syn.synold4s.control.dialog.GoogleMapMonitoringDialog;
import com.syn.synold4s.control.dialog.GoogleMapTowerDialog;
import com.syn.synold4s.control.dialog.GoogleMapWatchDialog;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class GoogleMapUtils {

    //不稳定 经常报异常
    public static String getAddressFromLocation(Context context, LatLng latLng) {
        Geocoder geocoder = new Geocoder(context, Locale.CHINESE);
        List<Address> addresses;
        try {
            addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
            if (addresses != null && !addresses.isEmpty()) {
                Address address = addresses.get(0);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                    sb.append(address.getAddressLine(i)).append(",");
                }
                sb.append(address.getCountryName()).append(",");
                sb.append(address.getAdminArea()).append(",");
                sb.append(address.getLocality()).append(",");
                sb.append(address.getSubLocality());
                return sb.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void uiSettings(GoogleMap map, boolean isLocationEnable) {
        // 地图设置
        UiSettings uiSettings = map.getUiSettings();
        uiSettings.setZoomControlsEnabled(false); // 显示缩放按钮
        uiSettings.setMyLocationButtonEnabled(isLocationEnable); // 显示定位按钮
        uiSettings.setCompassEnabled(false); // 显示指南针
        uiSettings.setMapToolbarEnabled(false); // 显示地图工具栏
        uiSettings.setRotateGesturesEnabled(true); // 允许旋转手势
        uiSettings.setScrollGesturesEnabled(true); // 允许滚动手势
        uiSettings.setTiltGesturesEnabled(true); // 允许倾斜手势
        uiSettings.setZoomGesturesEnabled(true); // 允许缩放手势
        uiSettings.setScrollGesturesEnabledDuringRotateOrZoom(true); // 允许在旋转或缩放时滚动手势
        uiSettings.setIndoorLevelPickerEnabled(true); // 显示室内层选择器
    }

    public static void createMarkers(GoogleMap map, Context context, MapAllDeviceDto device) {
        String iconName = device.getIcon();
        if (iconName == null || iconName.isEmpty()) {
            if (codeWatch.equals(device.getTag())) {
                iconName = "watch_offline";
            } else if (codeLock.equals(device.getTag())) {
                iconName = "unlock_offline";
            } else if (code360.equals(device.getTag())) {
                iconName = "car_offline";
            } else if (codeUnSafeAct.equals(device.getTag())) {
                if (device.getState() == 1) {
                    iconName = "tower_online";
                } else {
                    iconName = "tower_offline";
                }
            } else if (codeDriver.equals(device.getTag())){
                iconName = "sg830_01_offline";
            }
        }
        if (iconName == null) {
            iconName = "";
        }
        if ("SG830-01_offline".equals(iconName)){
            iconName = "sg830_01_offline";
        }
        int resId = context.getResources().getIdentifier(iconName.toLowerCase(), "drawable", context.getPackageName());
//        int resId = 0;
//        try {
//            Class<?> resClass = Class.forName(context.getPackageName()+".R$drawable");
//            Field field = resClass.getField(iconName);
//            resId = field.getInt(null);
//        } catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException e ) {
//            Log.d("TAG", "createMarkers: 本地icon資源未找到");
////            ToastUtil.showToast("本地icon資源未找到");
//        }
        if (resId != 0) {
            map.addMarker(new MarkerOptions()
                            .icon(BitmapDescriptorFactory.fromResource(resId))
                            .position(new LatLng(device.getLat(), device.getLng())))
                    .setTag(new String[]{device.getTag(), device.getId(), iconName});
        } else {
            Log.d("TAG", "createMarkers: 本地icon資源未找到 " + iconName);
        }
    }

    public static int getDrawableIcon(int battery, String icon) {
        int lockImageSrc = 0;
        if (battery > 100) {
            lockImageSrc = R.drawable.lock_charging;
        } else {
            switch (icon) {
                case "lock_offline_close":
                    lockImageSrc = R.drawable.lock_offline_close_big;
                    break;
                case "lock_offline_open":
                    lockImageSrc = R.drawable.lock_offline_open_big;
                    break;
                case "lock_online_open":
                    lockImageSrc = R.drawable.lock_online_open_big;
                    break;
                case "lock_online_close":
                    lockImageSrc = R.drawable.lock_online_close_big;
                    break;
            }
        }
        return lockImageSrc;
    }

    public static void showDialog(FragmentActivity context, MapAllDeviceDto data, String tagCode) {
        if (codeLock.equals(tagCode)) {
            showLockDialog(context, data);
        } else if (codeWatch.equals(tagCode)) {
            showWatchDialog(context, data);
        } else if (code360.equals(tagCode)) {
            showMonitoringDialog(context, data);
        } else if (codeTowerCrane.equals(tagCode)) {
            showTowerDialog(context, data);
        } else if (codeDriver.equals(tagCode)){
            showDriverDialog(context, data);
        }
    }

    private static void showLockDialog(FragmentActivity context, MapAllDeviceDto data) {
        new GoogleMapLockDialog(context, data).show(context.getSupportFragmentManager());
    }

    private static void showMonitoringDialog(FragmentActivity context, MapAllDeviceDto data) {
        new GoogleMapMonitoringDialog(context, data).show(context.getSupportFragmentManager());
    }

    private static void showWatchDialog(FragmentActivity context, MapAllDeviceDto data) {
        new GoogleMapWatchDialog(context, data).show(context.getSupportFragmentManager());
    }

    private static void showTowerDialog(FragmentActivity context, MapAllDeviceDto data) {
        new GoogleMapTowerDialog(context, data).show(context.getSupportFragmentManager());
    }

    public static void showDriverDialog(FragmentActivity context, MapAllDeviceDto data) {
        new GoogleMapDriverDialog(context, data).show(context.getSupportFragmentManager());
    }

    public static void showSOSDialog(FragmentActivity context) {
//        new SOSAlertDialog(context).show(context.getSupportFragmentManager());
    }
}
